package com.xbongbong.crm.abstracts;

import com.xbongbong.crm.handle.analysis.DefaultListAnalysisDataHandler;
import com.xbongbong.crm.help.CrmTeamPermissionHelp;
import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.BusinessListHelp;
import com.xbongbong.parent.help.PublicGroupHelp;
import com.xbongbong.paas.lowcode.CustomButtonService;
import com.xbongbong.paas.pojo.ButtonPojo;
import com.xbongbong.parent.pojo.GroupConditionPoJo;
import com.xbongbong.paas.pojo.dto.ColumnSetListDTO;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.pojo.vo.PaasFormDataEsListVO;
import com.xbongbong.paas.service.UserConfigExplainService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.parent.abstracts.AbstractBusinessListService;
import com.xbongbong.parent.pojo.list.ConditionListPojo;
import com.xbongbong.parent.pojo.list.DataAnalysisListPojo;
import com.xbongbong.pro.detailtab.vo.team.TeamListBatchPermissionVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.enums.lowcode.ButtonTypeEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.sys.model.UserModel;
import com.xbongbong.util.AddressUtil;
import com.xbongbong.util.ApproveUtil;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/03/11 11:14
 */
@Slf4j
public abstract class AbstractCrmListQryService extends AbstractBusinessListService {
    @Resource
    private UserConfigExplainService userConfigExplainService;
    @Resource
    private UserModel userModel;
    @Resource
    private PublicGroupHelp publicGroupHelp;
    @Resource
    private DefaultListAnalysisDataHandler defaultListAnalysisDataHandler;
    @Resource
    private CrmTeamPermissionHelp crmTeamPermissionHelp;
    @Resource
    private BusinessListHelp businessListHelp;
    @Resource
    private CustomButtonService customButtonService;


    @Override
    public void setSortMap(FormDataListDTO formDataListDTO) throws XbbException {
        Map<String, String> sortMap = businessListHelp.getSortMap(formDataListDTO.getSubBusinessType(), formDataListDTO.getBusinessType(), formDataListDTO.getFormId(), formDataListDTO.getAppId(), formDataListDTO.getCorpid(), formDataListDTO.getListGroupId(), formDataListDTO.getUserId(), formDataListDTO.getSortMap());
        formDataListDTO.setSortMap(sortMap);
    }

    @Override
    public void setFixedAndInvisible(ColumnSetListDTO columnSetListDTO, List<FieldAttrEntity> list) throws XbbException {
        userConfigExplainService.setFixedAndInvisible(columnSetListDTO, list);
    }

    @Override
    public void explainFieldSort(ColumnSetListDTO columnSetListDTO, List<FieldAttrEntity> list) throws XbbException {
        userConfigExplainService.explainFieldSort(columnSetListDTO, list);
    }



    /**
     * 获取团队的权限
     * @param userVO
     * @return
     */
    protected List<ConditionsEntityExt> getTeamUserPermission(UserVO userVO) {
        List<ConditionsEntityExt> conditionList = new ArrayList<>();
        Integer dataPermission = userModel.getDataPermission(userVO, false);
        List<String> userIdIn = userModel.getSubIdListByDataPermission(dataPermission, userVO.getUserId(), userVO.getCorpid(), BasicConstant.ZERO,false);
        ConditionsEntityExt conditionsEntityExt = new ConditionsEntityExt();
        conditionsEntityExt.setSymbol(ConditionEnum.IN.getSymbol());
        conditionsEntityExt.setValue(new ArrayList<>(userIdIn));
        conditionsEntityExt.setAttr(FieldTypeEnum.TEAM_USER.getAlias());
        conditionsEntityExt.setFieldType(FieldTypeEnum.TEAM_USER.getType());
        conditionList.add(conditionsEntityExt);
        return conditionList;
    }

    protected ConditionListPojo setCommonCondition(ConditionListPojo conditionListPojo) throws XbbException {
        List<List<ConditionsEntityExt>> shouldCondition = new ArrayList<>();

        // 分组里有个should的condition，就离谱
        GroupConditionPoJo groupConditionPoJo = publicGroupHelp.packageGroupList(conditionListPojo, shouldCondition);
        List<ConditionsEntityExt> groupConditionList = groupConditionPoJo.getConditionList();
        conditionListPojo.getConditionList().addAll(groupConditionList);
        conditionListPojo.setShouldConditionList(groupConditionPoJo.getShouldConditionList());
        // 审批中数据
        ApproveUtil.filterApprovalData(conditionListPojo.getBusinessType(), conditionListPojo.getSubBusinessType(), conditionListPojo.getDefaultGroup(), conditionListPojo.getListGroupId(), conditionListPojo.getConditionList());
        // 地址检索
        AddressUtil.addressSearchShouldCondition(conditionListPojo.getShouldConditionList());
//        AddressUtil.addressSearchCondition(conditionListPojo.getConditionList(), conditionListPojo.getShouldConditionList());
        return conditionListPojo;
    }



    @Override
    public List<PaasFormDataEntityExt> dingtalkDataAnalysis(DataAnalysisListPojo dataAnalysisListPojo) throws XbbException {
        try {
            List<PaasFormDataEntityExt> formDataList = dataAnalysisListPojo.getFormDataList();

            FormDataListDTO formDataListDTO = new FormDataListDTO(dataAnalysisListPojo.getCorpid(), dataAnalysisListPojo.getFormId(), dataAnalysisListPojo.getSaasMark(), dataAnalysisListPojo.getBusinessType(), dataAnalysisListPojo.getSubBusinessType(), dataAnalysisListPojo.getDistributorMark(), dataAnalysisListPojo.getUserVO(), dataAnalysisListPojo.getAnalysisAmountOfThousandBitsWithOriginalValue());

            PaasFormDataEsListVO formDataListVO = new PaasFormDataEsListVO();
            formDataListVO.setPaasFormDataESList(formDataList);
            formDataListVO.setHeadList(dataAnalysisListPojo.getExplainList());
            formDataListVO.setForm(dataAnalysisListPojo.getForm());
            defaultListAnalysisDataHandler.analysisDataList(formDataListDTO, formDataListVO);
            defaultListAnalysisDataHandler.analysisAmountOfThousandBits(formDataListVO, false);
            // 摘要和标题的封装
            formDataListVO = ExplainUtil.getValueByData(formDataListVO, formDataListDTO.getFormMap(), dataAnalysisListPojo.getFormIdAndHeadListMap());
            return formDataListVO.getPaasFormDataESList();
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            log.error("com.xbongbong.crm.abstracts.AbstractCrmListQryService.dingtalkDataAnalysis error", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }

    @Override
    protected List<ButtonPojo> getLowCodeButtonList(FormDataListDTO formDataListDTO) throws XbbException {
        return customButtonService.getLowCodeButtonList(formDataListDTO.getBusinessType(), formDataListDTO.getSubBusinessType(), formDataListDTO.getFormId(), formDataListDTO.getSaasMark(), formDataListDTO.getCorpid(), formDataListDTO.getLoginUser(), ButtonTypeEnum.LIST.getCode());
    }

    @Override
    public Map<String, Boolean> getTeamListPermissionMap(FormDataListDTO formDataListDTO, List<FieldAttrEntity> explainList) throws XbbException {
        TeamListBatchPermissionVO teamListBatchPermissionVO = crmTeamPermissionHelp.getTeamListBatchPermission(formDataListDTO, explainList);
        Map<String, Boolean> teamMap = teamListBatchPermissionVO.getTeamMap();
        if (Objects.isNull(teamMap)) {
            teamMap = new HashMap<>();
        }
        return teamMap;
    }


}
